package com.example.androidsqlitecontactdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //声明控件
    Button btnAdd,btnDelete,btnUpdate,btnSeleceName,btnSeleceAll;
    EditText edtName,edtTel;
    TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //找到控件
        btnAdd=this.findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(this);
        btnDelete=this.findViewById(R.id.btn_delete);
        btnDelete.setOnClickListener(this);
        btnUpdate=this.findViewById(R.id.btn_update);
        btnUpdate.setOnClickListener(this);
        btnSeleceName=this.findViewById(R.id.btn_selece_name);
        btnSeleceName.setOnClickListener(this);
        btnSeleceAll=this.findViewById(R.id.btn_selece_all);
        btnSeleceAll.setOnClickListener(this);

        edtName=this.findViewById(R.id.edt_name);
        edtTel=this.findViewById(R.id.edt_tel);
        tvMsg=findViewById(R.id.tv_msg);


    }

    //点击方法
    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.btn_add:
                Add();//添加方法
                showAll();//刷新页面方法
                break;
            case R.id.btn_delete:
                delete();//删除
                showAll();
                break;
            case R.id.btn_update:
                update();//修改
                showAll();
                break;
            case R.id.btn_selece_name:
                seleceName();//按照名字查询
                showAll();
                break;
            case R.id.btn_selece_all:
                showAll();//刷新页面（查询所有）
                break;
        }
    }

    //刷新页面方法(查询所有的方法)
    private void showAll() {
        StringBuffer sb = new StringBuffer();

        MyHelper helper = new MyHelper(MainActivity.this);
        SQLiteDatabase db = helper.getReadableDatabase();// 获取数据库操作对象db
        Cursor cursor = db.query("contacttb", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            long _id = cursor.getLong(0);
            String name = cursor.getString(1);
            String tel = cursor.getString(2);
            sb.append(_id + "   "+ name+"   "+ tel+ "\n");
        }
        cursor.close();
        String msg = sb.toString();// msg 保存所有查询到的记录数据
        tvMsg.setText(msg);
        db.close();
    }

    //添加方法
    private void Add() {
        //获取数据库对象
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();

        //将获取到的数据封装(准备记录数据)
        ContentValues values = new ContentValues();
        values.put("name",edtName.getText().toString());
        values.put("tel",edtTel.getText().toString());

        //保存在数据表中(添加数据)
        db.insert("contacttb",null,values);
        db.close();

        //添加后输入框数据清空
        edtName.setText("");
        edtTel.setText("");
    }

    //删除（根据名字删除）
    private void delete() {
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete("contacttb","name=?",new String[]{ edtName.getText().toString() });
        db.close();
    }

    //修改(根据名字修改电话号码)
    private void update() {
        // 获取用户数据
        String name = edtName.getText().toString();
        String tel = edtTel.getText().toString();
        //
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("tel",tel);

        db.update("contacttb" , values ,"name=?",new String[]{name});
        db.close();
    }

    //按照名字查询
    private void seleceName() {
        String name = edtName.getText().toString();

        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("contacttb", null, "name=?", new String[]{name}, null, null, null, "1");
        StringBuffer sb = new StringBuffer();
        if (cursor.moveToNext()){
            sb.append(cursor.getLong(0)+"   "+cursor.getString(1)+"   "+cursor.getString(2));
        }
        String msg = sb.toString();
        tvMsg.setText(msg);
        cursor.close();
        db.close();
    }
}